package com.stockz.ui.swing.charting.painter;

import java.awt.Graphics2D;
import java.awt.Rectangle;

import com.stockz.core.util.SpringUtil;
import com.stockz.ui.swing.charting.display.Displayable;
import com.stockz.ui.swing.charting.model.Shape;
import com.stockz.ui.swing.charting.painter.axis.InformationAxisPainter;
import com.stockz.ui.swing.charting.painter.axis.PriceAxisPainter;
import com.stockz.ui.swing.charting.painter.axis.TimeAxisPainter;
import com.stockz.ui.swing.charting.painter.axis.VolumeAxisPainter;
import com.stockz.ui.swing.charting.painter.chart.HLCBarChartPainter;
import com.stockz.ui.swing.charting.painter.chart.RectangleVolumePainter;
import com.stockz.ui.swing.charting.painter.chart.StockChartPainter;
import com.stockz.ui.swing.charting.painter.indicator.IndicatorControlPainter;
import com.stockz.ui.swing.charting.painter.indicator.IndicatorValuePainter;
import com.stockz.ui.swing.charting.util.UIChartingConstant.OVERLAY;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelState;
import com.stockz.ui.swing.charting.view.StockChartPanelStructure;

public class StockChartPanelPainter{
	
	public static synchronized void paint(Graphics2D g, StockChartPanel stockChartPanel, Rectangle rectangle) {
		StockChartPanelState state = stockChartPanel.getStockChartPanelState();
		StockChartPanelStructure structure = stockChartPanel.getStockChartPanelStructure();
		Rectangle drawingRectangle = state.getDrawingRectangle();
		Rectangle volumeRectangle = state.getVolumeRectangle();
		Rectangle priceRectangle = state.getPriceRectangle();
		
		if(state.isGridVisible()){
			GridPainter.paint(g, stockChartPanel, state.getDrawingRectangle());
		}
		
		RectangleVolumePainter.paint(g, stockChartPanel, state.getVolumeRectangle());
		StockChartPainter stockChartPainter = state.getStockChartPainter();
		if(stockChartPainter == null){
			stockChartPainter = SpringUtil.getApplicationContext().getBean(HLCBarChartPainter.class);
		}
		stockChartPainter.paint(g, stockChartPanel, priceRectangle);
		
		TimeAxisPainter.paint(g, stockChartPanel, new Rectangle(
				drawingRectangle.x,
				drawingRectangle.y+drawingRectangle.height,
				drawingRectangle.width,
				structure.getAxisTimeHeight()));
		
		InformationAxisPainter.paint(g, stockChartPanel, new Rectangle(
				drawingRectangle.x,
				drawingRectangle.y - structure.getAxisInformationHeight(),
				drawingRectangle.width,
				structure.getAxisInformationHeight()));
		
		PriceAxisPainter.paint(g, stockChartPanel, new Rectangle(
				priceRectangle.x - structure.getAxisPriceWidth(),
				priceRectangle.y,
				structure.getAxisPriceWidth(),
				priceRectangle.height));
		
		VolumeAxisPainter.paint(g, stockChartPanel, new Rectangle(
				volumeRectangle.x + volumeRectangle.width,
				volumeRectangle.y,
				structure.getAxisVolumeWidth(),
				volumeRectangle.height));
		
		for(Displayable displayable : state.getDisplayableSet()){
			displayable.paint(g);
			IndicatorControlPainter.paint(g, displayable);
			
			if(OVERLAY.INDEPENDENT.equals(displayable.getOverlay())){
				IndicatorValuePainter.paint(g, displayable);
				g.setColor(stockChartPanel.getStockChartPanelSkin().getAxisTimeColor());
				g.drawRect(displayable.getRectangle().x, displayable.getRectangle().y, displayable.getRectangle().width, displayable.getRectangle().height);
			}
		}
		
		for(Shape shape : state.getShapeSet()){
			if(shape.isVisible()){
				shape.paint(g, drawingRectangle);
			}
		}
		
		if(state.isCrosshairVisible()){
			CrosshairPainter.paint(g, stockChartPanel, state.getDrawingRectangle());
		}
	}
}
